'use strict';
app.controller('accountCtrl',['$scope','$cookies','utilService','accountService','toaster',function($scope,$cookies,utilService,accountService,toaster){
    $scope.systemUser = {};
    $scope.systemUser.platform=$cookies.loginType;
    $scope.systemUser.systemId=$cookies.systemId;

    // call server.
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'system/user',
            params:$scope.systemUser
        }).then(function(result){
        });
    }
    $scope.query();


    $scope.updateSystemUserStatus = function(id,status){
        $scope.systemUserId=id;
        $scope.status=status==1?0:1;

        accountService.updateSystemUserStatus($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.query();
                    break;
                case 624:
                    toaster.pop('waiting', "提示", result.message);
                    $state.go('app.404');
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

}]);

app.controller('createAccountCtrl',['$scope','$cookies','$state','accountService','toaster', function($scope,$cookies,$state,accountService,toaster){
    $scope.systemUser = {
        isUpdatePassword:1,
        gender:1,
        isChain:1
    };

    //新增加角色
    $scope.createAccount = function(){
        accountService.createSystemUser($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.account');
                    break;
                case 602:
                case 603:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
            //$state.go('app.account');
        });
    }


    //查询所有角色信息

    $scope.role={};
    $scope.role.platform=$cookies.loginType;
    $scope.role.systemId=$cookies.systemId;

    $scope.getAll=function(){
        accountService.getAll($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.roles=result.data;
                    break;
                default :
                    break;
            }
        })
    }

    $scope.getAll();

    //选中、
    $scope.checkItem=function(item){
        item.isSelect==0?item.isSelect=1:item.isSelect=0;
    }

}]);

app.controller('editAccountCtrl',['$scope','$cookies','$state','$stateParams','accountService','toaster', function($scope,$cookies,$state,$stateParams,accountService,toaster){
    $scope.systemUserId=$stateParams.systemUserId;

    $scope.systemUser = {
        isUpdatePassword:1,
        gender:1,
        systemUserId:$stateParams.systemUserId,
    };
    // query data.
    accountService.findById($scope).then(function(result){
        switch (result.status){
            case 200:
                $scope.systemUser = result.data;
                break;
            case 601:
                toaster.pop('waiting', "提示", result.message);
                $state.go('app.404');
                break;
            default :
                toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                break
        }

    });

    // update event.
    $scope.updateAccount = function(){

        accountService.updateSystemUser($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.account');
                    break;
                case 602:
                case 603:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }


    //查询所有角色信息
    $scope.role={};
    $scope.role.platform=$cookies.loginType;
    $scope.role.systemId=$cookies.systemId;
    $scope.role.systemUserId=$stateParams.systemUserId;

    $scope.getAll=function(){
        accountService.getAll($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.roles=result.data;
                    break;
                default :
                    break;
            }
        })
    }

    $scope.getAll();

    //选中、
    $scope.checkItem=function(item){
        item.isSelect==0?item.isSelect=1:item.isSelect=0;
    }

}]);

